package kburndown.client.service.data
{
	import kburndown.client.app.utils.DateHelper;

	public class UnitInfo
	{
		public var id: int;
		[Bindable]
		public var name: String;
		public var memberCount: int;
		public var style: String;
		public var posX: Number;
		public var posY: Number;
		public var tasks: Vector.<TaskInfo> = new Vector.<TaskInfo>();

		private var _tasks_done: Vector.<TaskInfo>;
		private var _tasks_undone: Vector.<TaskInfo>;

		public function UnitInfo()
		{

		}

		public function _sortTasks(sourceTasks: Vector.<TaskInfo>): Vector.<TaskInfo>
		{
			sourceTasks.sort(sortFun);
			function sortFun(x: TaskInfo, y: TaskInfo): Number
			{
				if (x.priority > y.priority)
					return 1;
				else if (x.priority < y.priority)
					return -1;
				else
					return 0;
			}
			return sourceTasks;
		}

		public function get tasks_done(): Vector.<TaskInfo>
		{
			if (!_tasks_done)
				filterTasks();
			return _tasks_done;
		}

		public function get tasks_undone(): Vector.<TaskInfo>
		{
			if (!_tasks_undone)
				filterTasks();
			return _tasks_undone;
		}

		public function taskForward(taskInfo: TaskInfo): void
		{
			var index: int = _tasks_undone.indexOf(taskInfo);
			if (index < _tasks_undone.length - 1)
			{
				_tasks_undone.splice(index, 1);
				_tasks_undone.splice(index + 1, 0, taskInfo);
			}
		}

		public function taskBackward(taskInfo: TaskInfo): void
		{
			var index: int = _tasks_undone.indexOf(taskInfo);
			if (index > 0)
			{
				_tasks_undone.splice(index, 1);
				_tasks_undone.splice(index - 1, 0, taskInfo);
			}
		}

		//把task标记为已完成 
		public function setTaskFinished(task: TaskInfo): void
		{
			task.finishedDate = DateHelper.getToday();
			var index: int = tasks_undone.indexOf(task);
			tasks_undone.splice(index, 1);
			tasks_done.push(task);
		}

		public function setMoveTask(taskId: int, anchorTaskId: int): void
		{
			if(taskId == anchorTaskId)
				return;
			
			var taskInfo: TaskInfo = getUndoTaskById(taskId);
			var taskIndex: int = _tasks_undone.indexOf(taskInfo);
			tasks_undone.splice(taskIndex, 1);
			if (anchorTaskId == 0)
			{
				tasks_undone.splice(0, 0, taskInfo);
			}
			else
			{
				var anchorTaskInfo: TaskInfo = getUndoTaskById(anchorTaskId);
				var anchorTaskIndex: int = _tasks_undone.indexOf(anchorTaskInfo);
				tasks_undone.splice(anchorTaskIndex + 1, 0, taskInfo);
			}
		}

		public function setTaskDeleted(task: TaskInfo): void
		{
			var taskIndex: int = _tasks_undone.indexOf(task);
			tasks_undone.splice(taskIndex, 1);

			taskIndex = tasks.indexOf(task);
			tasks.splice(taskIndex, 1);
		}
		
		public function setTaskReopen(task: TaskInfo, anchorTaskid: int): void
		{
			task.finishedDate = null;
			var index: int = tasks_done.indexOf(task);
			if(index != -1)
			{
				tasks_done.splice(index, 1);
			}
			
			tasks_undone.push(task);
			setMoveTask(task.id, anchorTaskid);
		}

		private function filterTasks(): void
		{
			_tasks_done = new Vector.<TaskInfo>();
			_tasks_undone = new Vector.<TaskInfo>();

			for (var i: int = 0; i < tasks.length; i++)
			{
				if (tasks[i].hasFinished)
					_tasks_done.splice(0, 0, tasks[i]);
				else
					_tasks_undone.push(tasks[i]);
			}
		}

		private function getUndoTaskById(id: int): TaskInfo
		{
			for (var i: int = 0; i < tasks_undone.length; i++)
			{
				if (tasks_undone[i].id == id)
					return tasks_undone[i];
			}
			return null;
		}


		public static function getUnitById(id: int): UnitInfo
		{
			var units: Vector.<UnitInfo> = ProjectInfo.instance.units;
			for (var i: int = 0; i < units.length; i++)
			{
				if (units[i].id == id)
					return units[i];
			}
			return null;
		}

		//------------------------------------------
		//			位置缓存
		//------------------------------------------
		private var _taskIndexMap: Object = {};

		public function saveTaskIndex(taskInfo: TaskInfo): void
		{
			var index: int = _tasks_undone.indexOf(taskInfo);
			_taskIndexMap[taskInfo.id] = index;
		}

		public function hasTaskIndexChange(taskInfo: TaskInfo): Boolean
		{
			if (_taskIndexMap[taskInfo.id] != null)
			{
				var index: int = _tasks_undone.indexOf(taskInfo);
				return !(int(_taskIndexMap[taskInfo.id]) == index);
			}
			return true;
		}

	}
}